package com.atguigu.demojuc.chap08;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolDemo_ {
    public static void main(String[] args) {
        //任务量大，都需要等待延时处理，OOM

        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
        System.out.println(new Date());

        for (int i = 0; i < 10; i++) {
            scheduledThreadPool.schedule(()->{
                System.out.println((Thread.currentThread().getName() + " 定时任务被执行" + new Date()));
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    scheduledThreadPool.shutdown();
                }
            },5, TimeUnit.SECONDS);
        }


    }
}
